<template>
  <div style="width: 100%">
    <a-card class="card" :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="报考院系">
                <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.bkyxsm" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{i.dwh+'-'+ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="报考专业">
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.bkzydm" @change='zyChange'>
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{i.zyh+'-'+ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
                <a-form-item label="报考研究方向">
                  <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.bkyjfxm">
                    <a-select-option v-for="i in selectMenu.yjfx" :key="i.id" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
              <a-form-item label="考生姓名">
              <a-input placeholder="请输入..." v-model="queryParam.ksxm"/>
            </a-form-item>
            </a-col>
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
                <a-form-item label="考生来源">
                  <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.kslym"  @search="handleSearch">
                    <a-select-option v-for="i in selectMenu.ksly" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
                <a-form-item label="毕业单位">
                  <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.dxbyxxdm" @change='ssChange'  :defaultActiveFirstOption="false"
    :showArrow="false"
    :filterOption="false" :notFoundContent="null" :value="value" @search="handleSearch">
                    <a-select-option v-for="i in lyData" :key="i.id" :value="i.xxdm">{{ i.xxmc }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
                <a-form-item label="学习形式">
                  <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zhxlxxxs">
                    <a-select-option v-for="i in selectMenu.xxlx" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :sm="{span:16}" :xl="{span:8}">
              <a-button type="primary" @click="loadData">查询</a-button>
              <!-- <a-button type="primary" @click="handleDownLoad">下载</a-button> -->
              <a-button @click="queryParam = {};loadData()" style="margin:0 10px">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'" />
              </a>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card class="card" :bordered="false">
      <a-alert :message="'总条数：' + pagination.total" type="info" show-icon />
      <a-table ref="table" size="default" rowKey="key" :columns="columns" :dataSource="data" :pagination="pagination" :loading="loading" 
        @change="handleTableChange">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
      </a-table>
    </a-card>
  </div>
</template>

<script>
  // import moment from 'moment'
  import {
    STable
  } from '~'
  import {
    mapState
  } from 'vuex'

import querystring from 'querystring';

let timeout;
let currentValue;



  const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']

  export default {
    components: {
      STable
    },
    data() {
      return {
        value:undefined,
        advanced: true,
        loading: false,
        data: [],
        lyData:[],
        // 查询参数
        queryParam: {
          bkyjfxm: '',
          bkyxsm: '',
          bkzydm: '',
          dxbyxxdm: '',
          ksfsm: '',
          kslym: '',
          zsxn:'',
          dxbyxxdmId:''
        },
        postParams: {},
        // 表头
        columns: [{
            title: '序号',
            scopedSlots: {
              customRender: 'serial'
            }
          },
          {
            title: '准考证号',
            dataIndex: 'ksbh'
          },
          {
            title: '姓名',
            dataIndex: 'ksxm'
          },
          {
            title: '报考院系',
            dataIndex: 'bkyxsmc'
          },
          {
            title: '报考专业代码',
            dataIndex: 'bkzydm'
          },
          {
            title: '报考专业',
            dataIndex: 'bkzymc'
          },
          {
            title: '研究方向',
            dataIndex: 'bkyjfxmc'
          },
          {
            title: '学习形式',
            dataIndex: 'zhxlxxxsmc'
          },
          {
            title: '毕业学校所在省份',
            dataIndex: 'dxbyxxszsf'
          },
          {
            title: '毕业学校代码',
            dataIndex: 'dxbyxxdm'
          },
          {
            title: '毕业学校名称',
            dataIndex: 'dxbyxxmc'
          },
          {
            title: '毕业专业代码',
            dataIndex: 'dxbyzydm'
          },
          {
            title: '毕业专业名称',
            dataIndex: 'dxbyxymc'
          }
        ],
        selectMenu: {
          yx: [], // 院系
          yjfx: [],
          zy: [],
          xxlx: [],
          ksly: [],
          ss: []
        },
        pagination: {
          current: 1,
          showTotal: (() => {return `共 ${this.pagination.total} 条`})
        }
      }
    },
    computed: {
      ...mapState({
        systemInfo: state => state.app.systemInfo,
        TYPE0126: state => state.app['TYPE0126'] // 年级
      })
    },
    created() {
      this.initSelectMenu()
      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })
    },
    methods: {
      initSelectMenu() {
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu = {
            yx: res[0].data
          }
          this.queryParam.bkyxsm = res[0].data[0].dwh
          this.getProssion(this.queryParam.bkyxsm)
          // 获取学习形式
          this.$api.base.dictionaryData('TYPE0441').then(res => {
            this.selectMenu.xxlx = res.data
          })
          // 获取考生来源
          this.$api.base.dictionaryData('TYPE0412').then(res => {
            this.selectMenu.ksly = res.data
          })
          // 毕业单位
          this.$api.base.schoolInfoList().then(res => {
            // this.selectMenu.ss = res.data.rows
            this.$set(this.selectMenu,'ss', res.data.rows)
            console.log(this.selectMenu.ss)
          })
          // 获取当前学年
          this.$api.base.getSchoolYear().then(res=>{
            this.queryParam.zsxn = res.data.masterAdmissionYear
             this.loadData({
              pageSize: 10,
              pageNum: 1
            })
          })

          this.loadData({
            pageSize: 10,
            pageNum: 1
          })
        })
      },

      // 加载数据
      loadData(parameter = {}) {
        if (!parameter.pageNum) {
          this.pagination.current = 1
          parameter.pageNum = 1
        }
        let params = Object.assign(this.queryParam, parameter)
        this.loading = true;
        this.$api.recruitstat.secondScoreQuery(params).then(res => {
          // console.log(res)
          const pagination = { ...this.pagination
          };
          pagination.total = 0
          if (res.data && res.data.total) {
            pagination.total = res.data.total;
          }
          this.data = []
          this.pagination = pagination;
          if (res.data && res.data.rows.length) {
            res.data.rows.map((item, index) => {
              this.selectMenu.xxlx.map((_item, _index) => {
                if (item.zhxlxxxs == _item.code) {
                  item.zhxlxxxsmc = _item.name
                }
              })
              return item;
            })
            this.data = res.data.rows;
          }
          this.loading = false;
        })
      },
      // 分页
      handleTableChange(pagination, filters, sorter) {
        console.log(pagination);
        const pager = { ...this.pagination
        };
        pager.current = pagination.current;
        this.pagination = pager;
        this.loadData({
          pageSize: 10,
          pageNum: pagination.current
        });
      },
       //隐藏搜索
       handleSearch (value) {
         this.lyData=[]
        this.selectMenu.ss.forEach(item=>{
          if(item.xxmc.indexOf(value)>=0){
            this.lyData.push(item)
          }
        })
      },
      // 选择院系
      yxChange(code) {
        this.$set(this.queryParam, 'bkzydm', '')
        this.$set(this.queryParam, 'bkyjfxm', '')
        this.$set(this.selectMenu, 'zy', [])
        this.$set(this.selectMenu, 'yjfx', [])
        if(code){
          this.getProssion(code)
        }
      },
      // 选择毕业单位
      ssChange(code){
        if(code){
          this.$api.base.schoolInfoListInfo(code).then(res=>{
            if(res.data){
              this.queryParam.dxbyxxdm = res.data.xxdm
            }
          })
        }else{
          this.dxbyxxdm = ''
        }
      },
      getProssion(code) {
        this.$api.base.majorAll({
          lsdwh: code
        }).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      },
      // 选择专业
      zyChange(code) {
        this.$set(this.queryParam, 'bkyjfxm', '')
        this.$set(this.selectMenu, 'yjfx', [])
        if(code){
          this.$api.base.searchDirectory({
            ssyxm: this.queryParam.bkyxsm,
            zsxn:this.queryParam.zsxn,
            sszym: code,
             xwccm: 2
          }).then(res => {
            this.$set(this.selectMenu, 'yjfx', res.data)
          })
        }
      },
      handleDownLoad() {
        this.$export('admissions/ssksxx/download2')
      }
    }
  }
</script>

<style lang="less" scoped>
  .card {
    margin-bottom: 20px;
  }
</style>
